package com.project.servlet;

import com.project.bean.UserBean;
import com.project.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller("test")
public class TestServlet extends HttpServlet {
    @Autowired
    private IUserService service;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置响应信息的MIME类型和编码集
        resp.setContentType("text/html;charset=utf-8");
        //向客户端输出信息
//        resp.getWriter().print("hello,中国," + service.getUserName());

        List<UserBean> list = service.findAll();
        String str = "<table border='1' width='80%'>";
        str += "<thead><tr><th>用户名</th><th>生日</th><th>电话</th><th>操作</th></tr></thead>";
        str += "<tbody>";
        for (UserBean user : list) {
            str += "<tr><td>" + user.getName() + "</td><td>"
                    + user.getBirthday() + "</td><td>" +
                    user.getPhone() + "</td>" +
                    "<td><a href='del?id=" + user.getId() + "'>删除</a>" +
                    "<a href='findById?id=" + user.getId() + "'>修改</a></td></tr>";
        }
        str += "</tbody></table>";
        str += "<a href='add.html'>添加</a>";

        resp.getWriter().print(str);

    }
}
